package com.punch.course.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.vo.course.StuCourseDetailVO;
import com.punch.common.vo.course.StuCourseVO;
import com.punch.common.vo.order.StudentCourseClassVO;
import com.punch.course.mapper.ClassMapper;
import com.punch.course.mapper.StudentCourseScheduleMapper;
import com.punch.course.model.ClassSchedule;
import com.punch.course.model.PunchClass;
import com.punch.course.model.StudentCourseSchedule;
import com.punch.course.service.ClassScheduleService;
import com.punch.course.service.StudentCourseScheduleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生课程实现层
 */
@Service
public class StudentCourseScheduleServiceImpl extends ServiceImpl<StudentCourseScheduleMapper, StudentCourseSchedule> implements StudentCourseScheduleService {

    @Resource
    private ClassMapper classMapper;

    @Resource
    private ClassScheduleService classScheduleService;

    /**
     * 学生上课课程列表
     *
     * @param studentId 学生id
     * @return StuCourseVO
     */
    @Override
    public StuCourseVO listStuCourse(Long studentId) {
        StuCourseVO stuCourseVO = new StuCourseVO();
        //查找前7天学生课表
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        LocalDateTime day7Before = LocalDateTime.of(LocalDate.now().minusDays(6), LocalTime.MIN);
        QueryWrapper<StudentCourseSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StudentCourseSchedule::getStudentId, studentId)
                .between(StudentCourseSchedule::getClassDate, day7Before, todayEnd);
        List<StudentCourseSchedule> listBefore = list(queryWrapper);
        if (CollUtil.isNotEmpty(listBefore)) {
            List<StuCourseDetailVO> lastWeek = new ArrayList<>();
            setRetList(listBefore, lastWeek);
            stuCourseVO.setLastWeek(lastWeek);
        }
        //查找后7天学生课表
        LocalDateTime day7After = LocalDateTime.of(LocalDate.now().plusDays(6), LocalTime.MIN);
        queryWrapper.clear();
        queryWrapper.lambda().eq(StudentCourseSchedule::getStudentId, studentId)
                .between(StudentCourseSchedule::getClassDate, todayEnd, day7After);
        List<StudentCourseSchedule> listAfter = list(queryWrapper);
        if (CollUtil.isNotEmpty(listAfter)) {
            List<StuCourseDetailVO> laterWeek = new ArrayList<>();
            setRetList(listAfter, laterWeek);
            stuCourseVO.setLaterWeek(laterWeek);
        }
        return stuCourseVO;
    }

    /**
     * 查找当天学生id参加课程列表
     *
     * @param schoolId  学校id
     * @param stuIdList 学生id列表
     * @param now       当前时间
     * @return List<StudentCourseSchedule>
     */
    @Override
    public List<StudentCourseSchedule> listStuByStuIds(Long schoolId, List<Long> stuIdList, LocalDate now) {
        return getBaseMapper().selectByStuIds(schoolId, stuIdList, now);
    }

    /**
     * 保存学生课表
     *
     * @param classId              课程id
     * @param studentCourseClassVO 学生信息
     * @return long
     */
    @Override
    public Long saveStudentCourseSchedule(Long classId, StudentCourseClassVO studentCourseClassVO) {
        PunchClass punchClassEX = classMapper.selectById(classId);
        List<StudentCourseSchedule> insertList = new ArrayList<>();
        QueryWrapper<ClassSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ClassSchedule::getCourseId, punchClassEX.getCourseId())
                .eq(ClassSchedule::getClassId, classId)
                .eq(ClassSchedule::getHolidayFlag, 0);
        List<ClassSchedule> classScheduleList = classScheduleService.list(queryWrapper);

        classScheduleList.forEach(classSchedule -> {
            StudentCourseSchedule studentCourseSchedule = new StudentCourseSchedule();
            //学生信息
            studentCourseSchedule.setStudentId(studentCourseClassVO.getStudentId());
            studentCourseSchedule.setStudentName(studentCourseClassVO.getStudentName());
            studentCourseSchedule.setStudentSex(studentCourseClassVO.getStudentSex());
            studentCourseSchedule.setStudentGrade(studentCourseClassVO.getStudentGrade());
            studentCourseSchedule.setStudentClass(studentCourseClassVO.getStudentClass());
            studentCourseSchedule.setGuardianMobile(studentCourseClassVO.getGuardianMobile());
            //课程信息
            studentCourseSchedule.setClassId(classId);
            studentCourseSchedule.setCourseId(punchClassEX.getCourseId());
            studentCourseSchedule.setCourseName(punchClassEX.getCourseName());
            studentCourseSchedule.setTeacherId(punchClassEX.getTeacherId());
            studentCourseSchedule.setSchoolId(punchClassEX.getCreateId());
            studentCourseSchedule.setSchoolName(punchClassEX.getCreateName());
            studentCourseSchedule.setClassRoomName(punchClassEX.getClassRoomName());
            studentCourseSchedule.setClassDate(classSchedule.getCourseStartDate().toLocalDate());
            studentCourseSchedule.setClassTime(punchClassEX.getClassTime());
            insertList.add(studentCourseSchedule);
        });
        this.saveBatch(insertList);
        return classId;
    }

    /**
     * 获取学生当前学期出勤率
     *
     * @param studentId 学生id
     * @param areaId    地区id,用于查询学期
     * @return double
     */
    @Override
    public Double attendanceRate(Long studentId, Integer areaId) {
        QueryWrapper<StudentCourseSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StudentCourseSchedule::getStudentId, studentId);
        // TODO: 2020/12/7 查找学期
        List<StudentCourseSchedule> list = list(queryWrapper);
        long arrivedCount = list.stream().filter(s -> s.getStatus() == 1).count();
        int count = list.size();
        return count == 0 ? 0d : NumberUtil.round(NumberUtil.div((double) arrivedCount, count), 2).doubleValue();
    }

    private void setRetList(List<StudentCourseSchedule> list, List<StuCourseDetailVO> day7) {
        StringBuilder sb = new StringBuilder();
        list.forEach(studentCourseSchedule -> {
            StuCourseDetailVO stuCourseDetailVO = new StuCourseDetailVO();
            stuCourseDetailVO.setStuCourseId(studentCourseSchedule.getId());
            stuCourseDetailVO.setCourseName(studentCourseSchedule.getCourseName());
            sb.append(studentCourseSchedule.getClassDate()).append(" ").append(studentCourseSchedule.getClassTime());
            stuCourseDetailVO.setCourseClassTime(sb.toString());
            stuCourseDetailVO.setSchoolName(studentCourseSchedule.getSchoolName());
            stuCourseDetailVO.setClassRoomName(studentCourseSchedule.getClassRoomName());
            day7.add(stuCourseDetailVO);
            sb.delete(0, sb.length());
        });
    }

}
